﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace docmgr.WinForm
{
    public partial class DirectoryList : DockContent
    {
        DataTable dtDocClass = null;

        Hashtable htDocClass = new Hashtable();
        public DirectoryList()
        {
            InitializeComponent();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

        }

        private void DirectoryList_Load(object sender, EventArgs e)
        {
            bindData();
        }

        private void DocClasslbx_MouseClick(object sender, MouseEventArgs e)
        {
            int ID = Convert.ToInt32(DocClasslbx.SelectedValue);

            DataTable dt = SQLHelper.ExecuteDataSet(SQLHelper.connString, String.Format("select Name from [Directory] where ID={0}", ID)).Tables[0];

            DocClassTbx.Text = "" + dt.Rows[0]["Name"];
        }

        private void DocClasslbx_SelectedValueChanged(object sender, EventArgs e)
        {
            
        }

        void bindData()
        {
            dtDocClass = SQLHelper.ExecuteDataSet(SQLHelper.connString, String.Format("select ID,Name from Directory where Type='DocClass'")).Tables[0];

            DocClasslbx.DataSource = dtDocClass;
            DocClasslbx.DisplayMember = "Name";
            DocClasslbx.ValueMember = "ID";
        }

        private void AddDocClass_Click(object sender, EventArgs e)
        {
            ///添加
            ///
            if (DocClassTbx.Text.Trim() == "")
            {
                MessageBox.Show("字段为空");
                return;
            }
            try
            { 
                SQLHelper.ExecuteSql(SQLHelper.connString, String.Format("insert [Directory] (Type,Name) values ('DocClass','{0}')", DocClassTbx.Text.Trim()));
                
            }
            catch(Exception ex111)
            {
                MessageBox.Show(ex111.StackTrace);
                return;
            }
            finally
            {
                
            }
            MessageBox.Show("添加成功！");
            bindData();
        }

        private void EditDocClass_Click(object sender, EventArgs e)
        {
            ///编辑 
            ///

            if(DocClassTbx.Text.Trim()=="")
            {
                MessageBox.Show("字段为空");
                return;
            }
            int id=-1;
            try
            {
                id = Convert.ToInt32(DocClasslbx.SelectedValue);
            }
            catch(Exception ex444)
            {
                MessageBox.Show("尚未选中任何选项！");
                return;
            }
            try
            {
                SQLHelper.ExecuteSql(SQLHelper.connString, String.Format("update [Directory] set Name='{0}' where ID={1}", DocClassTbx.Text.Trim(), id));
                
            }
            catch(Exception ex111)
            {
                MessageBox.Show(ex111.StackTrace);
                return;
            }
            finally
            {
               
            }
            MessageBox.Show("编辑成功！");
            bindData();
        }

        private void RmDocClass_Click(object sender, EventArgs e)
        {
            ///删除
            ///
            int id=-1;
            try
            {
                id = Convert.ToInt32(DocClasslbx.SelectedValue);
            }
            catch(Exception ex444)
            {
                MessageBox.Show("尚未选中任何选项！");
                return;
            }
            try
            {
                SQLHelper.ExecuteSql(SQLHelper.connString, String.Format("delete [Directory] where ID={0}",id));
            }
            catch (Exception ex111)
            {
                MessageBox.Show(ex111.StackTrace);
                return;
            }
            finally
            {
                
            }
            MessageBox.Show("删除成功！");
            bindData();
        }
    }
}
